#include "imagesavemanager.h"
#include <QDebug>
#include <QFile>
#include <QCoreApplication>
#include <QDir>
ImageSaveManager::ImageSaveManager()
{
    
}

void ImageSaveManager::SaveSummaryInfo(RUN_ENV re)
{
	QString currentdate = re.startTime.toString("yyyy-MM-dd");
	QString currentime = re.startTime.toString("hh_mm_ss");

	QString dirStr = QString("%1/TempImages/%2").arg(QCoreApplication::applicationDirPath()).arg(currentdate);
	if (re.lotId.length() > 0)
	{
		dirStr = QString("%1/%2").arg(dirStr).arg(re.lotId);
	}
	QDir dir;
	if (!dir.exists(dirStr))
	{
		dir.mkpath(dirStr);
		//qDebug() << "mkpath" << dir_str << res;
	}
	QString  filePath = dirStr.append("/AOI_Lot_Summary.txt");
	if (!filePath.isEmpty())
	{
		QFile file(filePath);
		if (file.open(QIODevice::WriteOnly | QIODevice::Text))
		{
			QTextStream out(&file);
			out << "            Lot Summary Report" << endl << endl << endl << endl << endl;
			out << "Lot ID						:  " << re.lotId << endl;
			out << "Package Name				:  " << re.packageName << endl;
			out << "Operator Name				:  " << re.operatorName << endl;
			out << "Date						:  " << re.startTime.toString("yyyy-MM-dd") <<" ~ "<< re.endTime.toString("yyyy-MM-dd") << endl;
			out << "Time						:  " << re.startTime.toString("hh:mm:ss") << " ~ " << re.endTime.toString("hh:mm:ss") << endl;
			out << "Software Version			:  " << QCoreApplication::applicationVersion() << endl << endl << endl;
			out << "==================================================================================================" << endl << endl << endl;
			out << "Summary" << endl;
			out << "------------------------" << endl;
			out << "Pass						:  " << re.successTimes << endl;
			out << "Fail						:  " << re.failTimes << endl;
			out << "Total						:  " << re.totalTimes << endl;
			if (re.totalTimes > 0)
			{
				out << "Pass Rate(%)				:  " << re.successTimes * 100 / re.totalTimes << "%" << endl << endl;

				out << "Fail Details" << endl;
				out << "------------------------" << endl;
				out << "Ball(Pad) Damage			:  " << re.ballDamageTimes << "  " << re.ballDamageTimes * 100 / re.totalTimes << "%" << endl;
				out << "Cap Damage					:  " << re.capDamageTimes << "  " << re.capDamageTimes * 100 / re.totalTimes << "%" << endl;
				out << "Substrate Damage			:  " << re.substrateDamageTimes << "  " << re.substrateDamageTimes * 100 / re.totalTimes << "%" << endl;
				int otherFail = re.failTimes - re.ballDamageTimes - re.capDamageTimes - re.substrateDamageTimes;
				out << "Wrong Image					:  " << otherFail << "  " << otherFail * 100 / re.totalTimes << "%" << endl;
			}
			file.close();
		}
		else
		{
			
		}
	}
}

void ImageSaveManager::AddImageList(QList<HalconCpp::HImage> imagePadList, QList<HalconCpp::HImage> imageCapList, QList<HalconCpp::HImage> imageSubList, QString lotName, QDateTime time, int result, HalconCpp::HImage imageResult, int chipType)
{
	if (m_thread->isFinished())
	{
		m_thread = new ImageSaveWorkerThread(this);
	}
	m_thread->AddImageList(imagePadList, imageCapList, imageSubList, lotName, time, result, imageResult, chipType);
	m_thread->start();
}
